import { decodeTgWebAppStartParam } from '~/utils';

export function validatePassword(_: any, v: any) {
  if (!v)
    return formResult($t('mfFbQx6d5lMlEhlQg5eV'));
  if (!/^.{8,20}$/.test(v))
    return formResult($t('3pmPAa711sGgTmbUNu4Y'));
  if (/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/.test(v))
    return formResult($t('aTqY6CSclH4yaPkBgWVkj'));
  if (!/^(?=.*[a-z])(?=.*[A-Z])|(?=.*[a-z])(?=.*\d)|(?=.*[A-Z])(?=.*\d).+$/.test(v))
    return formResult($t('vvN3hyzbeI7Mo7voWpaf'));
  // if (/^[A-Z]+$/i.test(v))
  //   return formResult($t('zYispPvfbgrfz8t4vewPu'));
  // if (/^\w+$/.test(v))
  //   return formResult($t('mxwSg3e0O_1B9UYznMXg'));
  return formResult();
}

export const icStorage = {
  key: 'ic',
  get() {
    return localStorage.getItem(this.key);
  },
  set(value: string) {
    localStorage.setItem(this.key, value);
  },
};

export function getInvitationCode() {
  const queryParams = new URLSearchParams(location.search);
  const hashParams = new URLSearchParams(location.hash.slice(1));
  const ic = queryParams.get('ic')
    || hashParams.get('ic')
    || decodeTgWebAppStartParam().ic
    || icStorage.get()
    || '';

  icStorage.set(ic);

  return ic;
}
